package parsing.yago;

import core.objects.Book;
import core.objects.Publisher;
import core.objects.Author;

public class WikipediaInfoParser extends YagoFileParser {

	private static final String WIKIPEDIA_INFO = "yagoWikipediaInfo.tsv";
	private static final int ESTIMATED_LINES_NUM = 43500000;
	
	private static final String LINKS_TO = "linksTo";
	private static final String HAS_WIKI = "hasWikipediaUrl";

	protected WikipediaInfoParser(String directory) {
		super(directory);
	}

	@Override
	protected String getFileName() {
		return WIKIPEDIA_INFO;
	}

	@Override
	protected int getEstimatedFileLinesNumber() {
		return ESTIMATED_LINES_NUM;
	}
	
	@Override
	protected void handleLineParsing(String[] line) {
		
		if (line.length != 3)
			return;
		
		/*line is <...> <linkTo / hasWikipediaUrl / ...> <..> */
		
		if (line[1].equals(HAS_WIKI)) {
			Book book = ParsingEnvironment.getBooks().get(line[0]);
			if (book != null) {
				book.setWikipediaURL(line[2]);
			}
		}
		if (line[1].equals(LINKS_TO)) {

			Book book = ParsingEnvironment.getBooks().get(line[2]);
			if (book != null) {
				
				Publisher publisher = ParsingEnvironment.getPublishers().get(line[0]);
				if (publisher != null) {
					//we have found publisher of book:
					book.addPublisher(publisher);
				} else {
					Author writer = ParsingEnvironment.getWriters().get(line[0]);
					if (writer != null) {
						book.addAuthor(writer);
					}
				}
			}

		}
	}

	@Override
	protected boolean isLineFromFileOkay(String[] line) {
		//too many line types for this file
		return true;
	}

}
